using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Volo.Abp.Application;
using Volo.Abp.AutoMapper;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Caching;
using Volo.Abp.Emailing;
using Volo.Abp.MailKit;
using Volo.Abp.Modularity;
using System.Net.Http;
using Matrix.Notification.Notifications;
using Matrix.Notification.Services;

namespace Matrix.Notification;

[DependsOn(
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule),
    typeof(AbpBackgroundJobsAbstractionsModule),
    typeof(AbpCachingModule),
    typeof(AbpEmailingModule),
    typeof(AbpMailKitModule),
    typeof(MatrixNotificationDomainModule),
    typeof(MatrixNotificationApplicationContractsModule)
)]
public class MatrixNotificationApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        // 注册自动映射配置
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<MatrixNotificationApplicationModule>();
        });

        // 配置推送通知选项
        Configure<PushNotificationOptions>(configuration.GetSection("PushNotification"));

        // 注册推送通知服务
        context.Services.AddTransient<IPushNotificationService, DefaultPushNotificationService>();

        // 配置WebHook通知选项
        Configure<WebHookNotificationOptions>(configuration.GetSection("WebHookNotification"));

        // 注册WebHook通知服务
        context.Services.AddTransient<IWebHookNotificationService, DefaultWebHookNotificationService>();

        // 配置通知管理器选项
        Configure<NotificationManagerOptions>(configuration.GetSection("NotificationManager"));

        // 配置通知路由选项
        Configure<NotificationRoutingOptions>(configuration.GetSection("NotificationRouting"));

        // 配置WebHook通知渠道选项
        Configure<WebHookNotificationChannelOptions>(configuration.GetSection("WebHookNotificationChannel"));

        // 注册通知管理器和路由服务
        context.Services.AddTransient<INotificationManager, DefaultNotificationManager>();
        context.Services.AddTransient<INotificationRoutingService, DefaultNotificationRoutingService>();

        // 配置渠道配置选项
        Configure<ChannelConfigurationOptions>(configuration.GetSection("ChannelConfiguration"));

        // 注册渠道配置服务
        context.Services.AddTransient<IChannelConfigurationService, DefaultChannelConfigurationService>();

        // 配置HttpClient
        ConfigureHttpClient(context);
    }

    private void ConfigureHttpClient(ServiceConfigurationContext context)
    {
        context.Services.AddHttpClient("FCM", client =>
        {
            client.BaseAddress = new Uri("https://fcm.googleapis.com/");
            client.Timeout = TimeSpan.FromSeconds(30);
        });

        context.Services.AddHttpClient("APNS", client =>
        {
            client.BaseAddress = new Uri("https://api.push.apple.com/");
            client.Timeout = TimeSpan.FromSeconds(30);
        });

        context.Services.AddHttpClient("HMS", client =>
        {
            client.BaseAddress = new Uri("https://push-api.cloud.huawei.com/");
            client.Timeout = TimeSpan.FromSeconds(30);
        });

        context.Services.AddHttpClient("WebHook", client =>
        {
            client.Timeout = TimeSpan.FromSeconds(60);
        });
    }
}